<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>token编辑</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/libs/layui2.2.5/css/layui.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/common.css" />
    <link rel="icon" href="href="${pageContext.request.contextPath}/static/libs/uiframe/frame/static/image/code.png" />
</head>
<body>
<form class="layui-form c3form" action="">
    <div class="layui-form-item">
        <input type="hidden" name="userId" value="${userId}"/>
        <label class="layui-form-label">用户名：</label>
        <div class="layui-input-block">
            <input type="text" disabled="disabled" name="userName" disabled="disabled"
                   lay-verify="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">秘钥：</label>
        <div class="layui-input-block">
            <input type="text" name="secretKey" lay-verify=""
                   placeholder="请输入秘钥" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">token：</label>
        <div class="layui-input-block">
            <input type="text" name="token" lay-verify="" disabled="disabled"
                   placeholder="请输入token" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="tokengenerate">生 成</button>
            <button class="layui-btn" lay-submit="" lay-filter="tokenadd">提 交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重 置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/libs/layui2.2.5/layui.js"></script>
<script>
    var secretKey = '${secretKey}';
    layui.use(['form','layer'],function(){
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        $("input[name='userName']").val('${userName}');
        $("input[name='secretKey']").val('${secretKey}');
        $("input[name='token']").val('${token}');

        //监听提交
        form.on('submit(tokenadd)', function(data){
            var formData = data.field;
            if(formData['secretKey'] == '' && formData['token'] != ''){
                layer.msg('请设置秘钥后重新生成token', {icon: 5,anim: 6});
                return false;
            } else if (formData['secretKey'] != '' && formData['secretKey'] != secretKey){
                layer.msg('请重新生成token', {icon: 5,anim: 6});
                return false;
            }


            $.ajax({
                type:'post',
                dataType:'json',
                url:'${pageContext.request.contextPath}/sys/user/tokensave',
                data:{
                    'userId': formData['userId'],
                    'secretKey': formData['secretKey'],
                    'token': formData['token']
                },
                success:function (data) {
                    if(data != null && data['status'] == 'OK'){
                        layer.msg(data.status);
                        window.parent.layIndex = null;
                        window.parent.closeAllLayer();
                    }else {
                        layer.msg('token保存出错', {icon: 5,anim: 6});
                    }
                },
                error:function (error) {
                    console.log(error);
                    layer.msg('token保存出错', {icon: 5,anim: 6});
                }
            });
            return false;
        });

        // 监听token生成
        form.on('submit(tokengenerate)', function(data){
            var formData = data.field;
            if(formData['secretKey'] == ""){
                $("input[name='token']").val('');
                return false;
            }
            secretKey = formData['secretKey'];
            $.ajax({
                type:'post',
                dataType:'json',
                url:'${pageContext.request.contextPath}/sys/user/tokencreate',
                data:{
                    'userId': formData['userId'],
                    'secretKey':formData['secretKey']
                },
                success:function (data) {
                    if(data != null && data.hasOwnProperty('token') && data['token'] != ''){
                        $("input[name='token']").val(data['token']);
                    }else {
                        layer.msg('token生成失败', {icon: 5,anim: 6});
                    }
                },
                error:function (error) {
                    console.log(error);
                    layer.msg('token生成失败', {icon: 5,anim: 6});
                }
            });
            return false;
        });
    })
</script>
</body>
</html>
